package com.yx.frame.authority;

import org.json.JSONObject;

import com.yx.frame.service.ActionManager;
import com.yx.frame.server.Application;
import com.yx.frame.service.Action;
import com.yx.frame.service.ServiceError;

public final class User extends JSONObject {

	boolean isAuthorization = false;

	public User(String jsonString) throws Exception {
		super(jsonString);
	}

	public User(String userId, String userName, String password,
			String userLicence) throws Exception {
		put("UserId", userId);
		put("UserName", userName);
		put("Password", password);
		put("UserLicence", userLicence);
	}

	public String getUserId() throws Exception {
		return getString("UserId");
	}

	public String getUserName() throws Exception {
		return getString("UserName");
	}

	public String getPassword() throws Exception {
		return getString("Password");
	}

	public String getUserLicence() throws Exception {
		return this.getString("UserLicence");
	}

	public boolean isAuthorization() {
		return isAuthorization;
	}

	public void setAuthorization(boolean isTrue) {
		isAuthorization = isTrue;
	}

	public String toSimpleString() {
		try {
			if (getUserId() != null) {
				return this.getUserId() + "," + this.getUserName();
			}
		} catch (Exception e) {
		}
		return this.getClass().getName();
	}
	private final String getUserLicenceMaskValue(int idx){
		String mask="0";
		try{
			mask=getUserLicence().substring(idx, idx+1);
		}catch(Exception e){}
		return mask;
	}

	public Action getPermitedAction(int actionId, ServiceError error)
			throws Exception {
		Action action = ActionManager.getInstace().getAction(actionId, error);
		if (error.isError())
			return null;
		int idx = action.getId();
		if("1".equals(getUserLicenceMaskValue(idx))){
			return action;
		}else{
			error.setErrorMessage("没有该操作的访问权限");
			return null;
		}		
	}

	public final void setUserMemStorage(String key, JSONObject obj, int timeOut)
			throws Exception {
		try {
			String md5Key = EncryptTools.MD5(key + "_User_" + getUserId());
			Application.getProvider().setCache(md5Key, timeOut,obj.toString());			
		} catch (Exception e) {
		}
	}

	public final JSONObject getUserMemStorage(String key) throws Exception {
		try {
			String md5Key = EncryptTools.MD5(key + "_User_" + getUserId());
			String mamcacheJsonString = Application.getProvider().getCached(md5Key);
			if (null == mamcacheJsonString || "".equals(mamcacheJsonString)) {
				return null;
			}
			return new JSONObject(mamcacheJsonString);
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}

	public final void removeUserMemStorage(String key) throws Exception {
		try {
			String md5Key = EncryptTools.MD5(key + "_User_" + getUserId());
			Application.getProvider().removeCached(md5Key);			
		} catch (Exception e) {
		}
	}

	public final void removeMemStoragedUser() throws Exception {
		try {
			String memcachedKey = "USER_MEMCACHED_UESERNAME_"
					+ this.getUserName();
			Application.getProvider().removeCached(memcachedKey);			
		} catch (Exception e) {
		}
	}
	public static final void removeMemStoragedUser(String cachedUserName) throws Exception {
		try {
			String memcachedKey = "USER_MEMCACHED_UESERNAME_"
					+ cachedUserName;
			Application.getProvider().removeCached(memcachedKey);			
		} catch (Exception e) {
		}
	}

}
